﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Threading;

using PEMS.Common;

namespace PEMS.Server
{
    public class ServerController
    {
        /// <summary>
        ///  legyártunk kevés mintaadatot
        /// </summary>
        public void mintaadat_gyartas()
        {
            lock (Intezmeny._lock)
            {

                Intezmeny.instance.felevList.Clear();
                Intezmeny.instance.felev_gyartas();
                Intezmeny.instance.emberList.Clear();
                Intezmeny.instance.ember_gyartas();
                Intezmeny.instance.targyList.Clear();
                Intezmeny.instance.targy_gyartas();

                Intezmeny.instance.felevList["12/13"].FelevJelentkezesgyartas(Intezmeny.instance.emberList["h1"]);

                Intezmeny.instance.felevList["12/13"].kurzus_gyartas(Intezmeny.instance.emberList["t1"], Intezmeny.instance.targyList["rfke"]);

                //Intezmeny.instance.felevList["12/13"].kurzusList["rfke_kurzus"].KurzusTagsaggyartas(Intezmeny.instance.emberlist["h1"]);

                Intezmeny.instance.felevList["12/13"].kurzusList["rfke_kurzus"].Vizsgaidopontgyartas();

                //Vizsga v=Intezmeny.instance.felevList["12/13"].kurzusList["rfke_kurzus"].vizsgaidopontList["1"].vizsgaGyartas(Intezmeny.instance.felevList["12/13"].kurzusList["rfke_kurzus"].kurzusTagList["h1"]);
                //Intezmeny.instance.felevList["12/13"].kurzusList["rfke_kurzus"].kurzusTagList["h1"].vizsgak.Add(v);
            }
            return;

        }

        /// <summary>
        /// adatok HDD-re írása
        /// </summary>
        public void adatKiiras(string path)
        {
            lock (Intezmeny._lock)
            {
                XmlSerializer serializer;
                TextWriter WriteFileStream;

                serializer = new XmlSerializer(typeof(SerializableDictionary<string, Ember>));
                WriteFileStream = new StreamWriter(path + "Emberek.xml", false);
                serializer.Serialize(WriteFileStream, Intezmeny.instance.emberList);
                WriteFileStream.Close();

                serializer = new XmlSerializer(typeof(SerializableDictionary<string, Felev>));
                WriteFileStream = new StreamWriter(path + "Felevek.xml", false);
                serializer.Serialize(WriteFileStream, Intezmeny.instance.felevList);
                WriteFileStream.Close();

                serializer = new XmlSerializer(typeof(SerializableDictionary<string, Targy>));
                WriteFileStream = new StreamWriter(path + "Targyak.xml", false);
                serializer.Serialize(WriteFileStream, Intezmeny.instance.targyList);
                WriteFileStream.Close();
            }

        }
        /// <summary>
        ///  HDD-ről adatok olvasása
        /// </summary>
        public void adatfelolvasas(string path)
        {
            XmlSerializer serializer;
            TextReader ReadFileStream;
            lock (Intezmeny._lock)
            {
                Intezmeny.instance.felevList.Clear();
                Intezmeny.instance.emberList.Clear();
                Intezmeny.instance.targyList.Clear();
                
                serializer = new XmlSerializer(typeof(SerializableDictionary<string, Targy>));
                ReadFileStream = new StreamReader(path + "Targyak.xml", false);
                Intezmeny.instance.targyList = (SerializableDictionary<string, Targy>)serializer.Deserialize(ReadFileStream);
                ReadFileStream.Close();

                serializer = new XmlSerializer(typeof(SerializableDictionary<string, Ember>));
                ReadFileStream = new StreamReader(path + "Emberek.xml", false);
                Intezmeny.instance.emberList = (SerializableDictionary<string, Ember>)serializer.Deserialize(ReadFileStream);
                ReadFileStream.Close();

                serializer = new XmlSerializer(typeof(SerializableDictionary<string, Felev>));
                ReadFileStream = new StreamReader(path + "Felevek.xml", false);
                Intezmeny.instance.felevList = (SerializableDictionary<string, Felev>)serializer.Deserialize(ReadFileStream);
                ReadFileStream.Close();

                //"referenciák" megújítása
                foreach (KeyValuePair<string, Felev> kvp in Intezmeny.instance.felevList)
                {
                    foreach (KeyValuePair<string, FelevJelentkezes> kvp1 in kvp.Value.jelentkezettList)
                    {
                        string tu = kvp1.Key;
                        kvp1.Value.hallgato = (Hallgato)Intezmeny.instance.emberList[tu];
                    }

                    foreach (KeyValuePair<string, Kurzus> kvp1 in kvp.Value.kurzusList)
                    {
                        string to = kvp1.Value.oktato.userName;
                        string tt = kvp1.Value.targy.id;
                        //string tf = kvp1.Value.felev.id;
                        kvp1.Value.oktato = (Tanar)Intezmeny.instance.emberList[to];
                        //kvp1.Value.felev = _felevList[tf];
                        kvp1.Value.targy = Intezmeny.instance.targyList[tt];

                        foreach (KeyValuePair<string, KurzusTagsag> kvp2 in kvp1.Value.kurzusTagList)
                        {
                            string tu = kvp2.Key;
                            kvp2.Value.felevJelentkezes = Intezmeny.instance.felevList[kvp.Key].jelentkezettList[tu];
                            //kvp2.Value.vizsgak.Clear();
                        }

                        foreach (KeyValuePair<string, VizsgaIdopont> kvp2 in kvp1.Value.vizsgaidopontList)
                        {
                            foreach (KeyValuePair<string, Vizsga> kvp3 in kvp2.Value.vizsgaList)
                            {
                                string ktagsagKey = kvp3.Key;
                                kvp3.Value.kurzustag = kvp1.Value.kurzusTagList[ktagsagKey];
                                kvp1.Value.kurzusTagList[ktagsagKey].vizsgak.Add(kvp3.Value);
                            }
                        }
                    }

                }

            }
            return;
        }

    }
}
